<?php

namespace App\Http\Controllers\Admin\Arts\Manage;

use App\Http\Controllers\Admin\ModuleController;
use App\Master\Framework\Extend\DataUtil;
use App\Master\Framework\View\AdminData;
use App\Models\Arts\WebMenusModel;
use App\Utils\AppResult;

class WebMenusController extends ModuleController
{

    /**
     * CURD方法前置处理
     * @param $method
     * @param $parameters
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function callAction($method, $parameters)
    {
        // 获取请求参数 用于二次处理
        $params      = $this->params;
        $this->model = WebMenusModel::query();

        // 根据不同方法处理一些业务
        switch ($method) {
            case 'index':
                // 请求类型是否为POST
                if ($this->isPost) {
                    // 条件语句
                    if (!empty($params['keyword'])) $this->model->where('name', 'like', "%{$params['keyword']}%");
                    if (!empty($params['status'])) $this->model->where('status', $params['status']);
                    if (!empty($params['type'])) $this->model->where('type', $params['type']);
                }
                // 视图文件目录
                $this->view = 'admin.arts.manage.web_menus.index';
                break;
//            case 'insert':
//                break;
//            case 'update':
//                break;
            default:
                break;
        }

        return parent::callAction($method, $parameters); // TODO: Change the autogenerated stub
    }

    public function __construct(array $attributes = [])
    {
        parent::__construct();
    }

    public function index()
    {
        if ($this->isPost) {
            $params = $this->params;

            $query = $this->model;

            // 自动排序
            if ($orderBy = AdminData::orderBy()) {
                foreach ($orderBy as $order){
                    $query->orderBy($order['sort'], $order['orderBy']);
                }
            }

            if (!isset($params['page'])){
                $list = $query->where('is_delete', 0)->select('id as value','name as label','parent_id')->get()->toArray();
                $return = DataUtil::recursion($list, 'value', 'parent_id');
            }else{
                $total = $list = $query->where('is_delete', 0);
                $return['rows'] = DataUtil::recursion($list->get()->toArray(), 'id', 'parent_id');
                $return['total'] = $total->count();
            }

            return AppResult::response200('success', $return);
        } else {
            return view($this->view);
        }
    }

    /**
     * 添加
     * @return string
     */
    public function insert()
    {
        $params = $this->params;
        $params['level'] = 1;
        if (!empty($params['parent_id'])){
            $parent_ids = is_array($params['parent_id']) ? $params['parent_id'] : [$params['parent_id']];
            $params['parent_id'] = end($parent_ids);
            if($parents = WebMenusModel::query()->select(['id', 'level', 'parent_ids'])->where('id', $params['parent_id'])->first())
            {
                $params['parent_ids'] = $parents['parent_ids']?($parents['parent_ids'].','.$params['parent_id']):$params['parent_id'];
                $params['level'] = $parents['level'] + 1;
            } else {
                return AppResult::response201('创建失败');
            }
        }
        // 初始化空数据
        $form               = DataUtil::field($params, $this->unset_fields);
        $form['show_child'] = $params['show_child'] == 'true' ? 1 : 0;
        $form['status']     = $this->statusValue;
        $form['created_at'] = time();
        if (!$this->model->insert($form)) {
            return AppResult::response201('创建失败');
        }
        return AppResult::response200('创建成功');
    }

    /**
     * 修改
     * @return string
     */
    public function update()
    {
        $params = $this->params;
        if (empty($params['id'])) {
            return AppResult::response203('操作超时！');
        }

        $params['level'] = 1;
        if (!empty($params['parent_id'])){
            $parent_ids = is_array($params['parent_id']) ? $params['parent_id'] : [$params['parent_id']];
            $params['parent_id'] = end($parent_ids);
            if($parents = WebMenusModel::query()->select(['id', 'level', 'parent_ids'])->where('id', $params['parent_id'])->first())
            {
                $params['parent_ids'] = $parents['parent_ids']?($parents['parent_ids'].','.$params['parent_id']):$params['parent_id'];
                $params['level'] = $parents['level'] + 1;
            } else {
                return AppResult::response201('创建失败');
            }
        }

        // 初始化空数据
        $form               = DataUtil::field($params, array_merge(['is_system','id'], $this->unset_fields));
        $form['show_child'] = $params['show_child'] == 'true' ? 1 : 0;
        $form['updated_at'] = time();
        if (!$this->model->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }
}